<template>
  <div class="moments">
    <gj-loadmore
    :top-method="loadTop"
    :bottom-method="loadBottom"
    :auto-fill="false"
    :show-bottom="LoadingBottomshow"
    ref="loadmore"
    v-if="dataLoaded">
      <template  v-for="item in items" >
        <smart-moment :item="item"/>
      </template>
      <div class="botTip" v-show="showBotTips" @click="goLogin">
        <img src="../assets/img/keguan@2x.png" alt="">
        <div class="botTipText">
            <span></span>
            <p>客官，注个册呗~</p>
            <p>可以刷出更多内容呢~</p>
        </div>
      </div>
      <div class="shade" ref="shade"></div>
    </gj-loadmore>
    <template v-else  v-for="mb in mbs">
      <skeleton/>
    </template>
  </div>
</template>

<script>
import SmartMoment from '../components/SmartMoment'
import RemoteDAO from '../tools/remoteDAO'
import GjLoadmore from '@/components/common/LoadMore'
import Skeleton from '@/components/common/Skeleton'
import {mapState, mapActions} from 'vuex'
import Toast from '../../../node_modules/mint-ui/packages/toast/index.js'
console.warn(Toast)
// import Utils from '@/tools/utils'

function logMethod (target, key, descriptor) {
  const method = descriptor.value

  descriptor.value = function () {
    console.log('calling:', key)
    return method.apply(this, arguments)
  }

  return descriptor
}

export default {
  data () {
    return {
      dataLoaded: false,
      mbs: 0,
      pageNum: 1,
      maxId: 0,
      items: [],
      showBotTips: false
    }
  },
  components: {
    SmartMoment,
    Skeleton,
    GjLoadmore
  },
  created () {
    let winH = document.documentElement.clientHeight - 45
    this.mbs = Math.ceil(winH / 184)
  },
  mounted () {
    Toast('woefjwo')
    this.getData().then(res => {
      this.dataLoaded = true
      let items = res.data.body.body.result || []
      this.items = items
      if (items.length > 0) {
        this.maxId = items[0].id
      } else {
        this.$BMessage.show('没有最新的消息了~')
      }
    }, err => {
      console.warn(err)
    })
  },
  activated () {
    window.scrollTo(0, 0)
  },
  methods: {
    ...mapActions(['setRemoveCardId']),
    loadTop () {
      this.pageNum = 1
      this.maxId = 0
      this.getData().then(res => {
        let items = res.data.body.body.result || []
        this.items = items
        this.$refs.loadmore.onTopLoaded()
        this.$refs.loadmore.reset()
        if (items.length > 0 && this.$route.path === '/') {
          this.$BMessage.show('刷新成功')
        } else if (this.$route.path !== '/settings') {
          this.$BMessage.show('没有最新的消息了~')
        }
      }, err => {
        console.warn(err)
      })
    },

    goLogin () {
      RemoteDAO.callNativeLogin()
        .then(resp => {
          window.location.reload()
        })
    },

    @logMethod
    loadBottom () {
      if (!localStorage.getItem('userCode')) {
        setTimeout(() => {
          this.showBotTips = true
          this.$refs.loadmore.onBottomAllLoaded()
        }, 300)
        return false
      }
      this.pageNum = this.pageNum + 1
      this.getData().then(res => {
        let items = res.data.body.body.result || []
        if (items.length === 0) {
          console.log('all loaded!')
          this.$refs.loadmore.onBottomAllLoaded()
        } else {
          this.items = [...this.items, ...items]
          this.$refs.loadmore.onBottomLoaded()
        }
        if (items.length > 0) {
          this.maxId = items[0].id
        }
      }, err => {
        console.warn(err)
      })
    },
    getData () {
      return RemoteDAO.queryDynamics({ pageNum: this.pageNum, pageSize: 10, maxId: this.maxId })
    }
  },
  computed: {
    ...mapState(['removeID']),
    minHeight () {
      return {
        minHeight: document.body.clientHeight - 62 + 'px'
      }
    },
    LoadingBottomshow () {
      return !!localStorage.getItem('userCode')
    }
  },
  watch: {
    removeID (val) {
      this.items = this.items.filter((item) => {
        return item.id !== this.removeID
      })
      this.setRemoveCardId(' ')
    },
    items (val) {
      this.$nextTick(() => {  // loadeMore触发dom更新，必须在nextTick来获取this.$refs.shade
        if (val.length < this.mbs) {
          this.$refs.shade.style.display = 'block'
        } else {
          this.$refs.shade.style.display = 'none'
        }
      })
    }
  }
}

</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style lang="less">
@import "../components/common/mixins.less";

.moments {
  padding-top: 45px;
  padding-bottom: 20px;
}

.info_name {
  .ellipsis();
	.info_name_font;
}
.v_logo {
	.vipLogo;
}

.fade {
  transform-origin: center top;
  opacity: 0;
  transform: scaleY(0);
  transition: all 0.5s ease;
}
.mint-msgbox {
  border-radius: 10px!important;
  width: 75% !important;
}
.mint-loadmore {
  position: relative;
}
.shade {
  display: none;
  position: absolute;
  left: 0;
  bottom: 0;
  background: #f1f3f6;
  width: 100%;
  height: 60px;
}
.botTip {
  display: flex;
  justify-content: center;
  align-items: center;
  margin-top: 20px;
  img {
    width: 72px;
    height: 83px;
    margin-left: 22px;
    margin-right: 17px;
  }
}
.botTipText {
  box-sizing: border-box;
  width: 230px;
  height: 61px;
  padding: 12px 55px 12px 25px;
  border: 1px solid #CDDBF0; /*no*/
  border-radius: 10px;
  background: #fff;
  color: #2E394E;
  position: relative;
  span {
    display: block;
    width: 0;
    height: 0;
    border-width: 10px;/*no*/
    border-style: solid;
    border-color: transparent #CDDBF0 transparent transparent;
    position: absolute;
    top: 33%;
    left: -20px; /*no*/
    &::before {
      content: '';
      display:block;
      width:0;
      height:0;
      border-width: 10px; /*no*/
      border-style:solid;
      border-color:transparent #fff transparent transparent;
      position:absolute;
      top:-10px; /*no*/
      left:-9px; /*no*/
    }
  }
  p {
    margin-bottom: 3px;
    font-size: 14px;
  }
}
</style>
